package wnhz.com.business.groupshop.ui;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.base.BaseFragment;
import wnhz.com.business.groupshop.fragment.PriaceFragment;
import wnhz.com.business.groupshop.fragment.ShaiXuanFragment;
import wnhz.com.business.groupshop.fragment.ZongheFragment;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.view.TranslucentActionBar;

public class HotXiangQingActivity extends BaseActivity implements ActionBarClickListener {

    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;
    @BindView(R.id.tv_zonghe)
    TextView tvZonghe;
    @BindView(R.id.iv_up)
    ImageView ivUp;
    @BindView(R.id.iv_down)
    ImageView ivDown;
    @BindView(R.id.ll_priace)
    LinearLayout llPriace;
    @BindView(R.id.tv_shaxuan)
    TextView tvShaxuan;
    @BindView(R.id.framelayout)
    FrameLayout framelayout;
    @BindView(R.id.tv_priace)
    TextView tvPriace;

    private int a = 0;
    private BaseFragment baseFragment;
    private ZongheFragment zongheFragment;
    private PriaceFragment priaceFragment;
    private ShaiXuanFragment shaiXuanFragment;
    private String  type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hot_xiang_qing);
        ButterKnife.bind(this);
        initview();
        zongheFragment = new ZongheFragment();
        priaceFragment = new PriaceFragment();
        shaiXuanFragment = new ShaiXuanFragment();
        actionbar.setData("", R.drawable.ic_lefft_white, null, 1, null, 1, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }

        if (savedInstanceState != null) {
            baseFragment = (BaseFragment) getSupportFragmentManager().getFragment(savedInstanceState, baseFragment.getClass().getSimpleName());
        } else {
            switch (type) {
                case "综合":
                    tvZonghe.setTextColor(getResources().getColor(R.color.red255));
                    tvPriace.setTextColor(getResources().getColor(R.color.text153));
                    ivUp.setImageResource(R.drawable.ic_classification_up);
                    ivDown.setImageResource(R.drawable.ic_classification_down);
                    tvShaxuan.setTextColor(getResources().getColor(R.color.text153));
                    baseFragment = zongheFragment.newInstance();
                    break;
                case "价格":
                    tvZonghe.setTextColor(getResources().getColor(R.color.text153));
                    tvPriace.setTextColor(getResources().getColor(R.color.red255));
                    if (a%2==0)
                    {
                        ivUp.setImageResource(R.drawable.ic_classification_up_red);
                        ivDown.setImageResource(R.drawable.ic_classification_down);
                        a=1;
                    }else
                    {
                        ivUp.setImageResource(R.drawable.ic_classification_up);
                        ivDown.setImageResource(R.drawable.ic_classification_down_red);
                        a=0;
                    }

                    tvShaxuan.setTextColor(getResources().getColor(R.color.text153));
                    baseFragment = priaceFragment.newInstance();
                    break;
                case "筛选":
                    tvZonghe.setTextColor(getResources().getColor(R.color.text153));
                    tvPriace.setTextColor(getResources().getColor(R.color.text153));
                    ivUp.setImageResource(R.drawable.ic_classification_up);
                    ivDown.setImageResource(R.drawable.ic_classification_down);
                    tvShaxuan.setTextColor(getResources().getColor(R.color.red255));
                    baseFragment = shaiXuanFragment.newInstance();
                    break;
            }
        }
        if (!baseFragment.isAdded()) {
            getSupportFragmentManager().beginTransaction().add(R.id.framelayout, baseFragment, baseFragment.getClass().getSimpleName()).show(baseFragment).commit();
        }
    }

    private void initview()
    {
        type = "综合";
    }

    @OnClick({R.id.tv_zonghe, R.id.ll_priace, R.id.tv_shaxuan})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_zonghe:
                tvZonghe.setTextColor(getResources().getColor(R.color.red255));
                tvPriace.setTextColor(getResources().getColor(R.color.text153));
                ivUp.setImageResource(R.drawable.ic_classification_up);
                ivDown.setImageResource(R.drawable.ic_classification_down);
                tvShaxuan.setTextColor(getResources().getColor(R.color.text153));
                turnToFragment(baseFragment.getClass(), ZongheFragment.class, null);
                break;
            case R.id.ll_priace:
                tvZonghe.setTextColor(getResources().getColor(R.color.text153));
                tvPriace.setTextColor(getResources().getColor(R.color.red255));
                if (a%2==0)
                {
                    ivUp.setImageResource(R.drawable.ic_classification_up_red);
                    ivDown.setImageResource(R.drawable.ic_classification_down);
                    a=1;
                }else
                    {
                        ivUp.setImageResource(R.drawable.ic_classification_up);
                        ivDown.setImageResource(R.drawable.ic_classification_down_red);
                        a=0;
                    }

                tvShaxuan.setTextColor(getResources().getColor(R.color.text153));
                turnToFragment(baseFragment.getClass(), PriaceFragment.class, null);
                break;
            case R.id.tv_shaxuan:
                tvZonghe.setTextColor(getResources().getColor(R.color.text153));
                tvPriace.setTextColor(getResources().getColor(R.color.text153));
                ivUp.setImageResource(R.drawable.ic_classification_up);
                ivDown.setImageResource(R.drawable.ic_classification_down);
                tvShaxuan.setTextColor(getResources().getColor(R.color.red255));
                turnToFragment(baseFragment.getClass(), ShaiXuanFragment.class, null);
                break;
        }
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {

    }

    public void turnToFragment(Class<? extends BaseFragment> fromFragmentClass, Class<? extends BaseFragment> toFragmentClass, Bundle args) {

        FragmentManager fm = getSupportFragmentManager();
        //被切换的Fragment标签
        String fromTag = fromFragmentClass.getSimpleName();
        //切换到的Fragment标签
        String toTag = toFragmentClass.getSimpleName();
        //查找切换的Fragment
        Fragment fromFragment = fm.findFragmentByTag(fromTag);
        Fragment toFragment = fm.findFragmentByTag(toTag);
        //如果要切换到的Fragment不存在，则创建
        if (toFragment == null) {
            try {
                toFragment = toFragmentClass.newInstance();
                toFragment.setArguments(args);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        //如果有参数传递，
        if (args != null && !args.isEmpty()) {
            toFragment.getArguments().putAll(args);
        }
        //Fragment事务
        FragmentTransaction ft = fm.beginTransaction();
        //设置Fragment切换效果
        ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out,
                android.R.anim.fade_in, android.R.anim.fade_out);
        /**
         * 如果要切换到的Fragment没有被Fragment事务添加，则隐藏被切换的Fragment，添加要切换的Fragment
         * 否则，则隐藏被切换的Fragment，显示要切换的Fragment
         */
        if (!toFragment.isAdded()) {
            ft.hide(fromFragment).add(R.id.framelayout, toFragment, toTag);
        } else {
            ft.hide(fromFragment).show(toFragment);
        }
        baseFragment = (BaseFragment) toFragment;
        //添加到返回堆栈
        //        ft.addToBackStack(tag);
        //不保留状态提交事务
        ft.commitAllowingStateLoss();
    }
}
